package com.example.dllo.moondog.information.strategy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseFragment;
import com.example.dllo.moondog.tools.GsonRequest;
import com.example.dllo.moondog.tools.ReadStringRequestUtf;
import com.example.dllo.moondog.tools.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by dllo on 16/3/2.
 */
public class StrategyFragment extends BaseFragment {
    private ListView listView;
    private StrategyAdapter adapter;
    private String url = "http://qt.qq.com/static/pages/news/phone/c10_list_1.shtml";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_strategy, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_strategy);
        adapter = new StrategyAdapter(getActivity());
        ReadStringRequestUtf readStringRequestUtf = new ReadStringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(final String response) {
                Gson gson = new Gson();
                final StrategyBeans strategyBeans = gson.fromJson(response, StrategyBeans.class);
                adapter.addData(strategyBeans.getList());
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        if (strategyBeans.getList().get(position).getImage_with_btn().equals(true)){
                            Intent intent = new Intent(getActivity(), StrategyActivity.class);
                            intent.putExtra("url", strategyBeans.getList().get(position).getArticle_url());
                            startActivityForResult(intent, 200);
                        }else {
                            Intent intent = new Intent(getActivity(), StrategyActivityTwo.class);
                            intent.putExtra("url3", strategyBeans.getList().get(position).getArticle_url());
                            startActivityForResult(intent, 200);
                        }

                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(readStringRequestUtf);

//        GsonRequest<StrategyBeans> request = new GsonRequest<StrategyBeans>(url, StrategyBeans.class, new Response.Listener<StrategyBeans>() {
//            @Override
//            public void onResponse(StrategyBeans response) {
//                adapter.addData(response.getList());
//                listView.setAdapter(adapter);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//
//        VolleySingleton.getInstance()._addRequest(request);

    }
}
